package org.bet.exception;

import java.util.Locale;

import org.springframework.context.MessageSource;

public class BETExceptionFactory {
	public static final String DETAILS_SEPARATOR = "\n";
	
	private MessageSource messageSource;

	public MessageSource getMessageSource() {
		return messageSource;
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
	
	public BETException createException(String code, Object[] params, Throwable cause){
		// TODO: How can we support i18N
		String message = messageSource.getMessage(code, params, Locale.US);
		BETException exception = new BETException(message);
		if(cause != null){
			exception.setDetails(message + DETAILS_SEPARATOR + cause);
		}
		else{
			exception.setDetails(message);
		}
		
		return exception;
	}
	
	public BETException createException(String code, Object[] params){
		return createException(code, params, null);
	}
}
